﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BenefitHelper.Sys;
namespace BenefitHelper.Data.Week
{
    public class CompanyAvgWeek
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 交易年
        /// </summary>
        public int Year { get; set; }
        /// <summary>
        /// 交易月
        /// </summary>
        public int Month { get; set; }
        /// <summary>
        /// 交易周
        /// </summary>
        public int Week { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }

        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }

        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }

        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }
        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }


        /// <summary>
        /// 删除当周所有记录
        /// </summary>
        /// <param name="weekId"></param>
        public void Delete(int week, int month, int year, DB.DBManager db)
        {
            var query = from t in db.CompanyAvgWeek where t.Week == week && t.Year == year select t;
            foreach (Data.Week.CompanyAvgWeek c in query)
            {
                db.CompanyAvgWeek.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存周均值
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitCompanyWeekAvg(int week, int month, int year, DB.DBManager db)
        {
            List<Data.ServerData.T_Account> serverAccounts = new List<ServerData.T_Account>();
            int connectionStringCount = System.Configuration.ConfigurationManager.ConnectionStrings.Count;
            for (int i = 0; i < connectionStringCount; i++)
            {
                string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[i].ConnectionString;
                if (System.Configuration.ConfigurationManager.ConnectionStrings[i].ProviderName.Equals("System.Data.OracleClient"))
                {
                    Common.ADO.Tools.OracleHelper.connectionString = connectionString;
                    List<BenefitHelper.Data.ServerData.T_Account> accountList = new BenefitHelper.Data.ServerData.T_Account().GetList();
                    foreach (BenefitHelper.Data.ServerData.T_Account _account in accountList)
                    {
                        serverAccounts.Add(_account);
                    }
                }
            }
            Data.Week.CompanyAvgWeek change = new Data.Week.CompanyAvgWeek();
            var query = (from t in db.CompanyAvgWeek where t.Week == week && t.Year == year select t).ToList();
            var query2 = (from t in db.AccountDayChange where t.TradeHistory.WPdate == week && t.TradeHistory.YPdate == year select t).ToList();
            var query3 = (from t in db.AccountDayAnalysis where t.TradeHistory.WPdate == week && t.TradeHistory.YPdate == year select t).ToList();
            if (query.Count() > 0)//有本周数据
            {
                if (query2.Count() > 0)
                {
                    query.First().BillCount = Math.Round(query2.Average(a => a.BillCount), 2);
                    query.First().DayCount = Math.Round(query2.Average(a => a.DayCount), 2);
                    query.First().Free = Math.Round(query2.Average(a => a.Free), 2);
                    query.First().LoseCount = Convert.ToInt32(query2.Average(a => a.LoseCount));
                    query.First().Profit = Math.Round(query2.Average(a => a.Profit), 2);
                    query.First().SumLost = Math.Round(query2.Average(a => a.SumLost), 2);
                    query.First().SumWin = Math.Round(query2.Average(a => a.SumWin), 2);
                    query.First().UsedMargin = Math.Round(query2.Average(a => a.UsedMargin), 2);
                    query.First().WinCount = Convert.ToInt32(query2.Average(a => a.WinCount));
                }
                if (query3.Count() > 0)
                {
                    query.First().AvgBillBenefit = Math.Round(query3.Average(a => a.AvgBillBenefit), 2);
                    query.First().AvgPostionTime = Math.Round(query3.Average(a => a.AvgPostionTime), 2);
                    query.First().MaxReturn = Math.Round(query3.Average(a => a.MaxReturn), 2);
                    query.First().OverStopLoss = Math.Round(query3.Average(a => a.OverStopLoss), 2);
                    query.First().PingJinPing = Convert.ToInt32(query3.Average(a => a.PingJinPing));
                    query.First().RaiseRate = Math.Round(query3.Average(a => a.RaiseRate), 2);
                    query.First().RiskCount = Convert.ToInt32(query3.Average(a => a.RiskCount));
                    query.First().Utilization = Math.Round(query3.Average(a => a.Utilization), 2);
                    query.First().WinRate = Math.Round(query3.Average(a => a.WinRate), 2);
                    query.First().Yield = Math.Round(query3.Average(a => a.Yield), 2);
                }
            }
            else
            {
                change.Month = month;
                change.Year = year;
                change.Week = week;
                if (query2.Count() > 0)
                {
                    change.BillCount = Math.Round(query2.Average(a => a.BillCount), 2);
                    change.DayCount = Math.Round(query2.Average(a => a.DayCount), 2);
                    change.Free = Math.Round(query2.Average(a => a.Free), 2);
                    change.LoseCount = Convert.ToInt32(query2.Average(a => a.LoseCount));
                    change.Profit = Math.Round(query2.Average(a => a.Profit), 2);
                    change.SumLost = Math.Round(query2.Average(a => a.SumLost), 2);
                    change.SumWin = Math.Round(query2.Average(a => a.SumWin), 2);
                    change.UsedMargin = Math.Round(query2.Average(a => a.UsedMargin), 2);
                    change.WinCount = Convert.ToInt32(query2.Average(a => a.WinCount));
                }
                if (query3.Count() > 0)
                {
                    change.AvgBillBenefit = Math.Round(query3.Average(a => a.AvgBillBenefit), 2);
                    change.AvgPostionTime = Math.Round(query3.Average(a => a.AvgPostionTime), 2);
                    change.MaxReturn = Math.Round(query3.Average(a => a.MaxReturn), 2);
                    change.OverStopLoss = Math.Round(query3.Average(a => a.OverStopLoss), 2);
                    change.PingJinPing = Convert.ToInt32(query3.Average(a => a.PingJinPing));
                    change.RaiseRate = Math.Round(query3.Average(a => a.RaiseRate), 2);
                    change.RiskCount = Convert.ToInt32(query3.Average(a => a.RiskCount));
                    change.Utilization = Math.Round(query3.Average(a => a.Utilization), 2);
                    change.WinRate = Math.Round(query3.Average(a => a.WinRate), 2);
                    change.Yield = Math.Round(query3.Average(a => a.Yield), 2);
                }
                db.CompanyAvgWeek.Add(change);
            }
            db.SaveChanges();
        }
    }
}